import base64
from typing import Any, Dict
import json

def preprocess(text: str) -> Dict[str, Any]:
    """"Extract JSON from the text and return it as a dictionary."""
    try:
        start_index = text.find('{')
        end_index = text.rfind('}')
        if start_index != -1 and end_index != -1:
            extracted_json = text[start_index:end_index + 1]
            res = json.loads(extracted_json)
        else:
            raise Exception("No JSON object found in the string.")
        return res
    except json.JSONDecodeError:
        return {
            "isItFormFillingRequest": False,
            "chat": text,
            "dataToFill": None,
            "urlWhereDataToFill": None
        }
    except Exception as e:
        return {"error": str(e)}

def get_base64_img(image_bytes: bytes) -> str:
    """Convert image bytes to base64 string."""
    return base64.b64encode(image_bytes).decode('utf-8')